#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zconf.h>
#include <pthread.h>
#include "sysinfo.h"
#include "procps.h"
#include "getsysinfo.h"



unsigned int rd_num;
unsigned long long rd_bytes;
double fs_ra;
double mem_ra;
double cpu_ra;

int main(int argc, char *argv[]) {

    pthread_t th_a, th_b, th_c;
    void *retval;
    pthread_create(&th_a, NULL, get_sys_info, 0);
    pthread_create(&th_c, NULL, data_write_to_csvfile, 0);

    if (argc > 1) {
        if(atoi(argv[1])==1){
            pthread_create(&th_b, NULL, show_sys_info, 0);
            pthread_join(th_b, &retval);
        }
    }

    pthread_join(th_a, &retval);
    pthread_join(th_c, &retval);
    return 0;
}

void get_sys_info() {
    while (1) {
        io_cnt(&rd_num, &rd_bytes);
        fs_ra = fs_rate();
        mem_ra = mem_rate();
        cpu_ra = cpu_rate();
    }
}


void show_sys_info() {

    while (1) {
        system("clear");
        printf("┌────────disk stat────────────┐\n");
        printf("│  number of read and write:        %u      │\n", rd_num);
        printf("│  number of read and write bytes:  %llu     │\n", rd_bytes);
        printf("└─────────disk stat───────────┘\n");

        printf("\n");

        printf("┌────────memory_stat───────────┐\n");
        printf("│   memory usage rate = %.2lf%%                    │\n", mem_ra);
        printf("└─────────memory_stat──────────┘\n");

        printf("\n");

        printf("┌────────memory stat───────────┐\n");
        printf("│  fs usage rate = %.2lf%%                          │\n", fs_ra);
        printf("└─────────memory stat──────────┘\n");

        printf("\n");

        printf("┌─────────cpu stat────────────┐\n");
        printf("│  cpu usage rate = %.2lf%%                          │\n", cpu_ra);
        printf("└─────────cpu stat────────────┘\n");

        sys_info_write_to_log();
        sleep(2);
        system("clear");
    }
}

void data_write_to_csvfile(void) {
    while (1) {
        sys_info_write_to_log();
        sleep(2);
    }
}


